// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var css_detail_info = require("../../../html/platform/financing/financingDetailInfo.css");
	var css_detail_info1 = require("../../../html/platform/financing/financingManage.css");
	var tools = require("tools");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var hml_delivery_create = require("./hml_delivery_create.html");
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10000,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var deliveryNoticeId;
	var noticeState;
    var left;
	var deliveryNoticeDetail;
	var clerk;
	var init = function(page) {
		deliveryNoticeId = page.params.deliveryNoticeId || base.getUrlParam("deliveryNoticeId");
		noticeState = page.params.noticeState || base.getUrlParam("noticeState");
		left = base.getUrlParam("left");
		if(left==null){
			left=1;
		}
		
		if (left == 1) {
			$(".acceptTasks").removeClass("none");
			$(".cancelTasks").removeClass("none");
			$(".signDelivery").addClass("none");
		} else if (left == 2) {
			$(".acceptTasks").addClass("none");
			$(".cancelTasks").removeClass("none");
			$(".signDelivery").removeClass("none");
		} else {
			$(".acceptTasks").addClass("none");
			$(".cancelTasks").removeClass("none");
			$(".signDelivery").addClass("none");
		}
		initEvent();
		inquireDeliveryNoticeDetail();
		router.init();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击放货配送
		$(".deliveryList").on("click", function() {
			router.route("deliveryNotice/deliveryList.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=" + noticeState, true);

		});
		
		//点击放货通知
		$(".deliveryNoticeList").unbind("click").click(function(){
			top.location.href = "../deliveryNotice/deliveryNoticeList.html?noticeState="+noticeState+"&left="+left;
		});

		//点击接受放货任务
		$(".acceptTasks").unbind("click").click(function() {
			dialog.showConfirmDialog("接受放货任务", "是否要接受此放货任务？", function() {

				base.fnAjax({
					action: "confirmDeliveryNotice.json",
					oData: {
						deliveryNoticeId: deliveryNoticeId
					}, // 传递参数
					fnSuccess: function(data) {
						if (data.status == 200) {
							base.alert(1, "接受放货任务成功");
							router.route("deliveryNotice/deliveryNoticeDetail.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=2", true);
						} else {
							base.alert(2, data.message);
						}
					}
				});
			});
		});
		
		//取消任务
		$("#cancelTasks").on("click",function(){
			dialog.showConfirmDialog("取消任务","是否要取消此放货任务？",function(){
				base.fnAjax({
					action : "cancelDeliveryNotice.json",
					oData : {
						deliveryNoticeId:deliveryNoticeId
					}, // 传递参数
					fnSuccess : function(data) {
						if (data.status == 200) {
							base.alert(1, "取消放货任务成功");
							location.href="../deliveryNotice/deliveryNoticeList.html";
						} else {
							base.alert(2, "信息获取失败");
						}
					}
				});
			});
		});

		//signDelivery点击生成放货配送
		$(".signDelivery").unbind("click").click(function() {
			$("#popdialog").html(hml_delivery_create);
			dialog.showDialog($("#delivery_dialog"), function() {});
			inquireLogisticsClerkPageList();
			showInventory();
			
			$("#clerkName").on("change", function() {
				$(".clerkNameInput").val($("#clerkName  option:selected").text());
				var clerkId = $("#clerkName").val();
				$.each(clerk.datas, function(i, val) {
					if (val.clerkId == clerkId) {
						$("#clerkMobile").val(val.clerkMobile);
					}
				});
			});
			
			//点击下发放货配送提交按钮
			$("#delivery_dialog").on("click", "#sure_deliveryNotice", function() {
				tools.protectDoubleClick();
				var inventoryIds = [];
				$.each($(".inventoryId:checked"), function(i, val) {
					inventoryIds[i] = val.value;
				});
				if (inventoryIds.length == 0) {
					base.alert(2, "请勾选货箱再下达放货配送！");
					return;
				}
				var clerkName = $(".clerkNameInput").val();
				var clerkId = $("#clerkName").val();
				var clerkMobile = $("#clerkMobile").val();
				base.fnAjax({
					action: "createDelivery.json",
					oData: {
						deliveryNoticeId: deliveryNoticeId,
						clerkMobile: clerkMobile,
						clerkId: clerkId,
						clerkName: clerkName,
						inventoryIds: inventoryIds
					}, //传递参数
					fnSuccess: function(data) {
						tools.cancelProtectDoubleClick();
						if (data.status == 200) {
							tools.cancelProtectDoubleClick();
							base.alert(1, "下达放货配送成功");
							dialog.hideDialog($("#delivery_dialog"));
							router.route("deliveryNotice/deliveryNoticeDetail.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=" + noticeState, true);
						} else {
							base.alert(2, data.message);
						}
					}
				});
	
			});
		});

	};
	//显示运输通知--货箱
	var showInventory = function() {
		base.fnAjax({
			action: "inquireWaitDeliveryBulkstockInventoryList.json",
			oData: {
				deliveryNoticeId: deliveryNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#inventoryTpl").html();
					var html = juicer(tpl, data);
					$("#inventoryBody").html(html);
					
					$("#delivery_dialog .inventoryIdAll").unbind("click").click(function(){
						if ($(this).prop("checked")) {
							$("#delivery_dialog .inventoryId").prop("checked", true);
						} else {
							$("#delivery_dialog .inventoryId").prop("checked", false);
						}
					});
					
					$("#delivery_dialog .inventoryId").unbind("click").click(function(){
						if ($("#delivery_dialog .inventoryId").length == $("#delivery_dialog .inventoryId:checked").length) {
							$("#delivery_dialog .inventoryIdAll").prop("checked", true);
						} else {
							$("#delivery_dialog .inventoryIdAll").prop("checked", false);
						}
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});


	}
	var inquireLogisticsClerkPageList = function() {
			base.fnAjax({
				action: "inquireLogisticsClerkPageList.json",
				oData: {
					pageIndex: pageIndex,
					pageSize: pageSize
				}, // 传递参数
				fnSuccess: function(data) {
					clerk = data;
					if (data.status == 200) {
						var tpl = $("#clerkTpl").html();
						var html = juicer(tpl, data);
						$("#clerkName").html(html);

					} else {
						base.alert(2, "信息获取失败");
					}
				}
			});
		}
		//获取放货通知明细(放货通知)
	var inquireDeliveryNoticeDetail = function() {
		base.fnAjax({
			action: "inquireDeliveryNoticeDetail.json",
			oData: {
				deliveryNoticeId: deliveryNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				deliveryNoticeDetail = data;
				if (data.status == 200) {
					var tpl = $("#financingDetail").html();
					var html = juicer(tpl, data);
					$("#deliveryDetlailMainContent").html(html);

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});